import CryptoJS from "crypto-js";
// import { Base64 } from 'js-base64';
import { GlobalBase64 } from "./base64s";
import { rawinflate } from "./rawinflate";
import { rawdeflate } from "./rawdeflate";

//default Key
let key = "EGCNPCV1";
//default iv
let iv = "CNPCV1EG";

function getDesEncryptString(data, _key, _iv) {
  let key = CryptoJS.enc.Utf8.parse(_key);
  let iv = CryptoJS.enc.Utf8.parse(_iv);
  let encrypted = CryptoJS.DES.encrypt(data, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  return encrypted.toString();
}
function getDesDecryptString(encrypted, _key, _iv) {
  let key = CryptoJS.enc.Utf8.parse(_key);
  let iv = CryptoJS.enc.Utf8.parse(_iv);
  let decrypted = CryptoJS.DES.decrypt(encrypted, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  return decrypted.toString(CryptoJS.enc.Utf8);
}
function getDesEncrypt(data) {
  let encrypted = getDesEncryptString(data, key, iv);
  return encrypted;
}
function getDesDecrypt(data) {
  let decryptedStr = getDesDecryptString(data, key, iv);
  return decryptedStr;
}

// 获取请求数据（明文请求）
function GetRealDataByRequest(requestPlaindata) {
  return GetEncrtyDataOrRealDataByRequest(requestPlaindata, false, 0);
}
//获取请求数据（密文请求）
// encrytype请求加密方式 0：不加密，1：DES加密，2：DES后数据压缩+base64加密 3：数据压缩+base64后DES加密
function GetEncrtyDataByRequest(requestPlaindata, encrytype, key, iv) {
  return GetEncrtyDataOrRealDataByRequest(
    requestPlaindata,
    true,
    encrytype,
    key,
    iv,
  );
}
// 获取加密数据
// 请求服务CONTENT方法请调用该方法
// requestPlaindata:明文json字符串
// usesdEncryt:true：启用请求加密,false:关闭请求加密
// encrytype请求加密方式，usesdEncryt:true有效 0：不加密，1：DES加密，2：DES后数据压缩+base64加密 3：数据压缩+base64后DES加密
// 返回统一密文JSON格式的处理请求或正常的JSON数据请求
function GetEncrtyDataOrRealDataByRequest(
  requestPlaindata,
  usesdEncryt,
  encrytype,
  key,
  iv,
) {
  usesdEncryt = usesdEncryt.toString();
  if (usesdEncryt == "true") {
    //新发送JSON对象
    let jsonnew = {};
    let isEncry = false;
    jsonnew.encrytype = encrytype;
    jsonnew.accesstoken = JSON.parse(requestPlaindata).access_token;
    let requestEncrtyData = requestPlaindata;
    switch (encrytype) {
      case 1:
        //DES加密
        requestEncrtyData = getDesEncryptString(requestPlaindata, key, iv);
        isEncry = true;
        break;
      case 2:
        //DES加密
        let encrydata = getDesEncryptString(requestPlaindata, key, iv);
        //对加密后的数据进行压缩
        requestEncrtyData = GlobalBase64.Base64.toBase64(
          rawdeflate.RawDeflate.deflate(GlobalBase64.Base64.utob(encrydata)),
        );
        isEncry = true;
        break;
      case 3:
        //DES加密
        let compressdata = GlobalBase64.Base64.toBase64(
          rawdeflate.RawDeflate.deflate(
            GlobalBase64.Base64.utob(requestPlaindata),
          ),
        );
        //对加密后的数据进行压缩
        requestEncrtyData = getDesEncryptString(compressdata, key, iv);
        isEncry = true;
        break;
    }
    //开启是否加密

    jsonnew.isencry = isEncry;
    //json对象自动创建KEY值data.VALUE值是经过加密的字符串
    jsonnew.data = requestEncrtyData;
    //将发送JSON对象转换为字符串
    let newjsonstr = JSON.stringify(jsonnew);
    return newjsonstr;
  } else {
    return requestPlaindata;
  }
}

//获取解密数据
//responseCipherdata 密文JSON格式统一标准对象
//返回明文JSON数据
function GetDecryptDataByResponse(responseCipherdata, key, iv) {
  //统一返回的JSON数据格式（加密）并转换为对象
  let responsejson = JSON.parse(responseCipherdata);
  // try {
  let newjsonstr = "";
  if (responsejson.isencry || responsejson.isuseddataencryption) {
    let method = responsejson.encrytype;
    if (method == undefined) {
      method = responsejson.dataencryptioninfo.encrymethod;
    }
    //   let method =
    //     responsejson.encrydata == undefined
    //       ? responsejson.dataencryptioninfo.encrymethod
    //       : responsejson.encrytype;
    //判断统一返回的JSON数据格式加密类型
    switch (method) {
      case 0:
      default:
        newjsonstr = responsejson.data;
        break;
      case 1:
        newjsonstr = getDesDecryptString(responsejson.data, key, iv);
        break;
      case 2:
        let comprssdata = GlobalBase64.Base64.btou(
          rawinflate.RawDeflate.inflate(
            GlobalBase64.Base64.fromBase64(responsejson.data),
          ),
        );
        newjsonstr = getDesDecryptString(comprssdata, key, iv);

        break;
      case 3:
        let decrydata = getDesDecryptString(responsejson.data, key, iv);
        newjsonstr = GlobalBase64.Base64.btou(
          rawinflate.RawDeflate.inflate(
            GlobalBase64.Base64.fromBase64(decrydata),
          ),
        );
        break;
    }
  } else {
    newjsonstr = responsejson.data;
  }
  let realresponsejson = JSON.parse(newjsonstr);
  let realresponsestr = JSON.stringify(realresponsejson);
  return realresponsestr;
  // } catch (err) {
  //   return responseCipherdata;
  // }
}

//请求JSON对象返回JSON对象
function getServerResponses(res, key, iv) {
  let response = res;
  try {
    if (res.isuseddataencryption || res.isencry) {
      let ss = JSON.stringify(res);
      response = JSON.parse(GetDecryptDataByResponse(ss, key, iv));
    }
  } catch (error) {
    throw error;
  }
  return response;
}
//POST请求JSON返回JSON
function getRequestJson(requestjson, usesdEncryt, encrytype, key, iv) {
  let requestPlaindata = JSON.stringify(requestjson);
  return JSON.parse(
    GetEncrtyDataOrRealDataByRequest(
      requestPlaindata,
      usesdEncryt,
      encrytype,
      key,
      iv,
    ),
  );
}

export { getRequestJson, getServerResponses, getDesDecrypt, getDesEncrypt };
